import axios from 'axios'
const transformResponse = function (res) {
    /* eslint-disable*/
    res = res.replace(/\"product_id\":(\d+)/g, '"product_id":"$1"')

    res = res.replace(/\"size_info_template_id\":(\d+)/g, '"size_info_template_id":"$1"')
    // LOG.info(res)
    return JSON.parse(res)
}
const service = axios.create({
    // baseURL: env.BASE_URL,
    timeout: 200000,
    transformResponse
})
service.interceptors.request.use(
    async (config) => {
        // const paramData = await getParamData(config)
        // console.log('config234', config)
        // console.log('paramData', paramData)
        config.baseURL = config.baseURL || 'http://127.0.0.1:8080'
        if (config.baseURL == 'https://fxg.jinritemai.com/') {
            if (config.param) {
                config.param._lid = String(Date.now()).substr(5) + String(Math.random()).substr(2, 4)
                config.param.appid = 1
                config.param._ = new Date().getTime()
            } else {
                // config.param = {
                //     _lid: String(Date.now()).substr(5) + String(Math.random()).substr(2, 4),
                //     appid: 1,
                //     _: new Date().getTime()
                // }
            }
        }

        if (!/\?/.test(config.url) && config.baseURL == 'https://fxg.jinritemai.com/') { //  (config.method === 'get' || config.method === 'GET')
            let str = splitJoint(config.param)
            config.url += str
        }
        config.headers['Content-Type'] = 'application/json'
        if (config.method === 'post' || config.method === 'POST') {
            // config.data = {
            //     ...paramData,
            //     // ...config.data,
            // }
        }
        // console.log('config',config.url, config.method ,config.data)
        // delete config.headers['Origin']
        // delete config.headers['Origin']
        console.log(' config.headers[]', config.headers)

        return config
    },
    (error) => {
        // console.log(error, '123456798***---32165489797')
        Promise.reject(error)
    }
)

function splitJoint(obj) {
    let arr = []
    for (let key in obj) {
        arr.push(key + '=' + obj[key])
    }
    return arr.length > 0 ? '?' + arr.join('&') : ''
}
service.interceptors.response.use(
    (response) => {
        // console.log('response',  response.data)
        return response.data
    },
    (error) => {
        // console.log('ererer', error, 'erroyer')
        return Promise.reject(error)
    }
)
export default service